var banner2  = new liwe.module ( "banner2" );

function BannerBox ( dest_div, tipo, box_img )
{
	var self = this;

	this._name = dest_div;
	this._dest_div = $el ( dest_div );

	if ( ! this._dest_div )
	{
		console.error ( 'BannerBox: dest_div not found (%s)', dest_div );
		return;
	}

	this._items = [];
	this._cur = 0;

	this._last_id = 0;

	this._timer = 3000;
	if ( tipo [ 'playtime' ] > 0 ) this._timer = tipo [ 'playtime' ] * 1000 ;
	this._tipo = tipo;
	this._banner = {};

	this._base_index = 0; 
	this._upper = 0;
	this._lower = 0;

	this._base_index = this._dest_div.style.zIndex;

	this._upper = this._base_index + 10;
	this._lower = this._base_index + 5;

	// fix by keeper
	//this._el1 = 'bn-box-el1-' + tipo [ 'id' ];
	//this._el2 = 'bn-box-el2-' + tipo [ 'id' ];
	this._el1 = null;
	this._el2 = null;

	this._box_img = box_img && box_img || 'bn-box-img';

	var items = $c ( 'div', 'bn-el', this._dest_div );
	var create = false;

	if ( items.length == 0 )
	{
		tipo [ '_banner' ] = '';
		this._dest_div.innerHTML = String.formatDict ( banner2.templates [ 'bn-box' ], tipo );
		create = true;
	}

	items = $c ( 'div', 'bn-el', this._dest_div );

	this._el1 = items [ 0 ];
	this._el2 = items [ 1 ];
	this._last_id = this._el1.getAttribute ( 'rel' );
	//this._current = create ? this._el2 : this._el1;
	if ( create ) this.refresh ();

	var tipo = tipo [ 'id' ];

	/*
	liwe.events.add ( this._el1, 'mouseover' ,function () { banner2.stop_roll ( tipo ); } );
	liwe.events.add ( this._el1, 'mouseout' ,function () { banner2.start_roll ( tipo ); } );
	liwe.events.add ( this._el2, 'mouseover' ,function () { banner2.stop_roll ( tipo ); } );
	liwe.events.add ( this._el2, 'mouseout' ,function () { banner2.start_roll ( tipo ); } );
	*/
	liwe.events.add ( this._el1, 'mouseover' ,function () { banner2.stop_roll ( self._name ); } );
	liwe.events.add ( this._el1, 'mouseout' ,function () { banner2.start_roll ( self._name ); } );
	liwe.events.add ( this._el2, 'mouseover' ,function () { banner2.stop_roll ( self._name ); } );
	liwe.events.add ( this._el2, 'mouseout' ,function () { banner2.start_roll ( self._name ); } );
	this._can_roll = true;

	this._timeoutid = setTimeout ( function () { self.refresh (); }, this._timer );
};

BannerBox.prototype.refresh = function ()
{
	var self = this;
	banner2.ajax ( { action: 'banner2.ajax.show_banner', last_id: this._last_id, id_tipo: this._tipo [ 'id' ] }, function ( data )
	{
		var b = data [ 'banner' ];

		if ( self._last_id == b [ 'id_banner' ] ) 
		{
			self._can_roll = false;
			return;
		}
		self._last_id = b [ 'id_banner' ];

		self._can_roll =  true;

		var old = self._current;

		if ( self._current == self._el1 )
		{
			self._current = self._el2;
		}
		else
		{
			self._current = self._el1;
		}
		b [ 'w' ] = self._tipo [ 'w' ];
		b [ 'h' ] = self._tipo [ 'h' ];

		if ( old && old.firstChild )
			liwe.events.del ( old.firstChild, 'click', banner2.click );

		if ( b [ 'ext' ] == 'swf' )
		{
			b [ 'swf' ] = String.format ( '/site/banner/media/%s.swf', b [ 'id_banner' ] );
			tpl = banner2.templates [ 'bn-box-flash' ];
		}
		else
		{
			tpl = banner2.templates [ self._box_img ];
		}

		$( self._current, String.formatDict ( tpl, b ) );
		banner2.dispatchEvent ( 'stats-view', null, b );	
	
		self.animate ( old, self._current );
		self._timeoutid = setTimeout ( function () { self.refresh (); }, self._timer );
	}, function () { console.warn ( 'Error loading new banner!' ); } );
};

BannerBox.prototype.animate  = function ( old, current ) 
{
	var effect = this._tipo [ 'effect' ];

	if ( old )
	{
		var els = $c ( 'div', 'bn-click', old );
		if ( els.length )
			els [ 0 ].style.display = 'none';

		var els = $c ( 'div', 'bn-click', current );
		if ( els.length )
			els [ 0 ].style.display = 'block';
	}

	switch ( effect ) 
	{
		case 0:
			if ( old ) liwe.fx.set_opacity ( old, 0 );
			liwe.fx.set_opacity ( current, 100 );
			break;
		case 1:
			liwe.fx.fade_in ( current );
			liwe.fx.fade_out ( old );
			break;
		case 2:
			liwe.fx.push ( old, current, 'right' );
			break;
		case 3:
			liwe.fx.push ( old, current, 'top' );
			break;
		case 4:
			liwe.fx.push ( old, current, 'left' );
			break;
		case 5:
			liwe.fx.push ( old, current, 'bottom' );
			break;
	};
	
};

banner2.render = function ( dest_div, id_tipo, name, template )
{
	var req = { 'action' : 'banner2.ajax.get_tipo' };
	if ( typeof ( name ) != 'undefined' )
	{
		req [ 'name' ] = name;
	} else
	{
		req [ 'id_tipo' ] = id_tipo;
	}

	//banner2.ajax ( { action : 'banner2.ajax.get_tipo', id_tipo : id_tipo }, function ( data )
	banner2.ajax ( req , function ( data )
	{
		var box = new BannerBox ( dest_div, data [ 'tipo' ], template );
		banner2._instances [ dest_div ] = box;
		//banner2.dispatchEvent ( 'stats-show', null, data ); 
		//banner2._instances [ id_tipo ] = box;
	} );
};


banner2.start_roll = function ( name )
{
	var b = banner2._instances [ name ];
	if (! b._can_roll ) return;
	if ( b._timeoutid ) return;
	b._timeoutid = setTimeout ( function () { b.refresh (); }, b._timer );
	
};

banner2.stop_roll = function ( name )
{
	
	var b = banner2._instances [ name ];
	if ( typeof ( b ) == "undefined" ) return;
	clearTimeout ( b._timeoutid );
	b._timeoutid = null;
};

banner2._instances = {};
banner2._children = {};


banner2.start_all = function ()
{
	banner2._instances.iterate ( function ( b ) 
	{
		b._can_roll = true;
		banner2.start_roll ( b._name );
	} );	
}

banner2.stop_all = function ()
{
	banner2._instances.iterate ( function ( b ) 
	{ 
		banner2.stop_roll(b._name );
		b._can_roll = false;
	} );
};


banner2.init = function ()
{
	banner2.load_templates ();
	//console.debug ( "banner  2  init" );
	banner2.valid_events ( [ 'stats-view', 'stats-click' ] );

	banner2.addEventListener ( 'stats-click', function ( e )
	{
		if ( banner2 && banner2.admin ) return;
		var params = e.params;
		params._uid = ( user && user._uid ) ? user._uid : -1;
		liwe.ga.event ( 'Banner', 'Click', String.formatDict ( 'id_banner: %(id_banner)s / id_tipo: %(id_tipo)s / link: %(link)s / uid: %(_uid)s', params ) ); 
	} );
	banner2.addEventListener ( 'stats-view', function ( e )
	{
		if ( banner2 && banner2.admin ) return;
		var params = e.params;
		params._uid = ( user && user._uid ) ? user._uid : -1;
		var data = 'id_banner: %(id_banner)s / id_gruppo: %(id_gruppo)s / id_tipo: %(id_tipo)s / banner: %(descr)s / uid: %(_uid)s';
		liwe.ga.event ( 'Banner', 'View', String.formatDict ( data, params ) ); 
	} );
};
	
banner2._get_children = function ( base_div, refresh )
{
	var elems = banner2._children.get ( base_div , null );

	if ( ! elems || refresh )
	{
		elems = $c ( 'div', 'bn-box', base_div );
		banner2._children [ base_div ] = elems;
	}
	return elems;
};

banner2.suspend = function ( base_div, roll, refresh )
{

	var elems = banner2._get_children ( base_div, refresh );
 		
	var box, cnt_id, c, l = elems.length;
	for ( c = 0; c < l; c++ )
	{
		try
		{
			cnt_id = elems [ c ].parentNode.id;
		} catch ( e ) {
			cnt_id = null;
		}

		//box = banner2._instances [ cnt_id ];
		//box._can_roll = ( command == "stop" ) ? false : true;
		if ( ! cnt_id ) continue;
		if ( roll == "stop" ) banner2.stop_roll ( cnt_id ); 
		if ( roll == "start" ) banner2.start_roll ( cnt_id ); 
	};
};

banner2._call_old = function ( id_banner, lnk )
{
	
	if ( lnk ) 
	{
		window.open ('/ajax.pyhp?action=banner2.ajax.click_banner&id_banner=' + id_banner);
		return;
	} 
	
	banner2.ajax ( { 'action': 'banner2.ajax.click_banner', 'id_banner' : id_banner } );
};

banner2._call = function ( id_banner, lnk, id_tipo )
{
	setTimeout ( function () { if ( lnk ) window.open ( lnk ); }, 50 );
	banner2.dispatchEvent ( 'stats-click', null, { 'id_banner': id_banner, 'link': lnk, 'id_tipo' : id_tipo } );
};

banner2.plugin = function ( el, params )
{
	if ( params.get ( 'id_tipo' ) )
	{
		el.innerHTML = '';
		banner2.render ( el, params [ 'id_tipo' ] );
	}
	else if ( params.get ( 'id' ) )
	{
		banner2.ajax ( { action: 'banner2.ajax.get_banner', id_banner: params [ 'id' ] }, function ( res )
		{
			var b = res.banner;
			if ( ! b )
			{
				console.error ( 'Banner with id %d not found!', params [ 'id' ] );
				return;
			}
			b [ 'id_banner' ] = b [ 'id' ];
			b [ 'banner' ] = b [ 'descr' ];
			el.innerHTML = String.formatDict ( banner2.templates [ 'bn-box-img' ], b );
		} );
	}
};

